package sg.edu.nus.iss.billing.gui;

//Tan Pic Cheng

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

import sg.edu.nus.iss.billing.controller.CustomerController;
import sg.edu.nus.iss.billing.entity.Customer;
import sg.edu.nus.iss.billing.entity.RecordState;
import sg.edu.nus.iss.billing.exception.ControllerException;
import sg.edu.nus.iss.billing.exception.ValidationException;
import sg.edu.nus.iss.billing.gui.component.ErrorDialog;
import sg.edu.nus.iss.billing.gui.component.ValidateJTextField;
import sg.edu.nus.iss.billing.localization.MessageKeyManager;
import sg.edu.nus.iss.billing.privilege.AdminRole;
import sg.edu.nus.iss.billing.validation.ValidationRule;

public class ViewCustomerPanel extends DefaultFunctionPanel {
	private static final long serialVersionUID = 1L;

	private static int HEIGHT = 30;

	private JLabel lblWarning = new JLabel();
	private JLabel lblCustHeader;
	private JLabel lblCustDetails;
	private CheckboxGroup cbg;
	private Checkbox ckNRIC;
	private Checkbox ckID;

	private Boolean ckState;

	private ValidateJTextField fieldCustNRIC;
	private String fieldCustNRICUpper;
	private JButton lookupButton;
	private JButton restoreButton;

	private JTabbedPane tabbedPane;
	private JPanel tabPanel;
	private JPanel custInfoPanel;
	private JPanel mobilePlanPanel;
	private JPanel digitalPlanPanel;
	private JPanel cablePlanPanel;
	private JPanel searchPanel;
	private JPanel p;

	// 4 panels to view customer's personal information
	private ViewCustomerFieldPanel custPanel;
	private ViewCustomerAddressPanel custAdd;
	private ViewCustomerContactPanel custContact;
	private ViewCustomerMiscPanel custMisc;

	private CustomerController customerController;
	private Customer cust;

	public ViewCustomerPanel() {

		customerController = new CustomerController();
		cust = SessionContext.getCustomer();
		if (cust != null && cust.getActive() == RecordState.ACTIVE) {
			super.init(null);
		} else {
			super.init("TOPONLY");
		}

	}

	public JPanel createTopPanel() {
		// Specify the top panel to display NRIC & Customer Account Number
		// search options
		searchPanel = new JPanel(null);

		lblCustHeader = new JLabel(
				MessageKeyManager.getMessage("label.custHeader"));
		Font f = new Font("Arial", Font.BOLD, 12);
		lblCustHeader.setFont(f);
		lblCustHeader.setBounds(10, 15, 200, HEIGHT);
		searchPanel.add(lblCustHeader);

		JPanel displayOptions = displaySearchOptions();
		displayOptions.setBounds(0, 25, 800, 80);
		searchPanel.add(displayOptions);
		return searchPanel;

	}

	private JPanel displaySearchOptions() {
		// TODO Auto-generated method stub
		p = new JPanel(null);
		p.setPreferredSize(new Dimension(800, 80));

		lblCustDetails = new JLabel(
				MessageKeyManager.getMessage("label.custDetails"));
		lblCustDetails.setBounds(10, 15, 150, HEIGHT);
		p.add(lblCustDetails);

		cbg = new CheckboxGroup();
		ckNRIC = new Checkbox(MessageKeyManager.getMessage("label.nric"), true,
				cbg);
		ckID = new Checkbox(MessageKeyManager.getMessage("label.custAcct"),
				false, cbg);
		ckNRIC.setBounds(180, 15, 250, HEIGHT);
		ckID.setBounds(450, 15, 250, HEIGHT);

		p.add(ckNRIC);
		p.add(ckID);

		fieldCustNRIC = new ValidateJTextField(
				MessageKeyManager.getMessage("label.custDetails"));
		fieldCustNRIC.setBounds(180, 50, 250, 25);
		p.add(fieldCustNRIC);

		lookupButton = new JButton(
				MessageKeyManager.getMessage("button.lookup"));
		lookupButton.setBounds(450, 50, 100, 25);
		lookupButton.addActionListener(new lookupListener());
		p.add(lookupButton);

		return p;
	}

	public JPanel createBottomPanel() {
		tabPanel = new JPanel();
		// Create the tab pages
		// createMobilePlanPanel();
		// createDigitalPlanPanel();
		// creaetCablePlanPanel();
		// Create a tabbed pane
		tabbedPane = new JTabbedPane();
		tabbedPane.setPreferredSize(new Dimension(800, 600));
		custInfoPanel = createCustomerInfoPanel();
		tabbedPane.addTab(
				MessageKeyManager.getMessage("label.customerInfoPanel"),
				custInfoPanel);
		// tabbedPane.addTab( "Mobile Voice Plan ", mobilePlanPanel );
		// tabbedPane.addTab( "Digital Voice Plan", digitalPlanPanel );
		// tabbedPane.addTab( "Cable Plan", cablePlanPanel );
		tabPanel.add(tabbedPane);

		return tabPanel;

	}

	public JPanel restoreCustomer() {
		JPanel restorePanel = new JPanel();

		restoreButton = new JButton(
				MessageKeyManager.getMessage("button.restore"));
		restorePanel.add("CENTER", restoreButton);

		ActionListener restore = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cust.setActive(RecordState.ACTIVE);
				try {
					customerController.editCustomer(cust);
				} catch (ControllerException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				refreshBottomPanel(createBottomPanel());
				searchPanel.remove(lblWarning);
				searchPanel.updateUI();
			}
		};

		restoreButton.addActionListener(restore);

		return restorePanel;
	}

	public JPanel createCustomerInfoPanel() {
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(0, 2));

		custPanel = new ViewCustomerFieldPanel(cust);
		custAdd = new ViewCustomerAddressPanel(cust);
		custContact = new ViewCustomerContactPanel(cust);
		custMisc = new ViewCustomerMiscPanel(cust);

		mainPanel.add(custPanel);
		mainPanel.add(custAdd);
		mainPanel.add(custContact);
		mainPanel.add(custMisc);

		return mainPanel;
	}

	/*
	 * public void ViewMobilePlanPanel() {
	 * 
	 * mobilePlanPanel = new ViewMobilePlanPanel();
	 * 
	 * }
	 */
	public Customer loadCustomerByNRIC(String nric) {

		Customer custObj = null;

		try {

			custObj = customerController.isNRICExist(nric);

		} catch (ControllerException ex) {

			ex.printStackTrace();

		}

		return custObj;

	}

	public Customer loadCustomerByID(String id) {

		Customer custObj = null;

		try {

			custObj = customerController.getCustomerByAccount(id);

		} catch (ControllerException ex) {

			ex.printStackTrace();

		}

		return custObj;

	}

	/*
	 * private Customer createDummyCust(){
	 * 
	 * Customer addCustObj = new Customer(); String add1 = "Blk 100A"; String
	 * add2 = "Jurong West Street 52"; String unit = "25-119"; int postal =
	 * 761596; Address address = new Address(add1, add2, unit, postal); String
	 * custID = UniqueKeyGenerator.createUniqueAccountNo();
	 * addCustObj.setId(custID); addCustObj.setNRIC("S9060176F");
	 * addCustObj.setSalutation("Miss"); addCustObj.setName("Apple Lim");
	 * addCustObj.setEmail("iss@nus.edu.sg");
	 * addCustObj.setBillingAddress(address);
	 * addCustObj.setContactNumber1("61234567");
	 * addCustObj.setContactNumber2("91234504");
	 * addCustObj.setContactNumber3("67777777");
	 * addCustObj.setInterests("Java Programming"); /* try {
	 * customerController.addCustomer(addCustObj); } catch (ControllerException
	 * e) { // TODO Auto-generated catch block e.printStackTrace(); }
	 * 
	 * return addCustObj;
	 * 
	 * }
	 */
	public class lookupListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

			Checkbox selectedCheckbox;
			selectedCheckbox = cbg.getSelectedCheckbox();

			if (selectedCheckbox == ckNRIC)
				ckState = true;
			else
				ckState = false;

			try {
				if (tabPanel != null) {
					tabPanel.removeAll();
					tabPanel.updateUI();
				}
				if (lblWarning.isShowing() == true) {
					searchPanel.remove(lblWarning);
					searchPanel.updateUI();
				}
				fieldCustNRICUpper = fieldCustNRIC.getText().toUpperCase().trim();
				fieldCustNRIC.setText(fieldCustNRICUpper);
				if (ckState == true) {
					fieldCustNRIC.setRules(ValidationRule.NOT_NULL,
							ValidationRule.NRIC);
					cust = loadCustomerByNRIC(fieldCustNRIC.getValidatedText());
					if (cust == null) {
						String msg = MessageKeyManager.getMessage("msg.addCustomerNewRecord",fieldCustNRIC.getText());
						lblWarning.setText(msg);
						lblWarning.setBounds(180, 100, 400, 20);
						lblWarning.setVerticalTextPosition(SwingConstants.BOTTOM);
						searchPanel.add(lblWarning);
						searchPanel.updateUI();
					} else if (cust != null
							&& cust.getActive() == RecordState.DELETED) {
						SessionContext.setCustomer(cust);						
						String msg = MessageKeyManager.getMessage("msg.addCustomerDeleteByNRIC",fieldCustNRIC.getText());
						lblWarning.setText(msg);
						lblWarning.setBounds(180, 100, 400, 20);
						lblWarning.setVerticalTextPosition(SwingConstants.BOTTOM);
						searchPanel.add(lblWarning);
						searchPanel.updateUI();
						if(SessionContext.getUser().getRole().getClass() == AdminRole.class)
						refreshBottomPanel(restoreCustomer());
					} else {
						SessionContext.setCustomer(cust);
						refreshBottomPanel(createBottomPanel());
					}
				} else {
					fieldCustNRIC.setRules(ValidationRule.NOT_NULL,
							ValidationRule.ACCOUNT_NUMBER);
					cust = loadCustomerByID(fieldCustNRIC.getValidatedText());

					if (cust == null) {
						String msg = MessageKeyManager.getMessage("msg.addCustomerNewRecord",fieldCustNRIC.getText());

						lblWarning = new JLabel(msg);
						lblWarning.setBounds(180, 100, 500, 20);
						lblWarning.setVerticalTextPosition(SwingConstants.BOTTOM);

						searchPanel.add(lblWarning);
						searchPanel.updateUI();

					} else if (cust != null
							&& cust.getActive() == RecordState.DELETED) {
						SessionContext.setCustomer(cust);
						String msg = MessageKeyManager.getMessage("msg.addCustomerDeleteByAccount",	fieldCustNRIC.getText());
						lblWarning.setText(msg);
						lblWarning.setBounds(140, 100, 550, 20);
						lblWarning.setVerticalTextPosition(SwingConstants.BOTTOM);
						searchPanel.add(lblWarning);
						searchPanel.updateUI();
						refreshBottomPanel(restoreCustomer());
					} else {
						SessionContext.setCustomer(cust);
						refreshBottomPanel(createBottomPanel());
					}
				}
				fieldCustNRIC.setText("");
			} catch (ValidationException ex) {
				ErrorDialog.showValidationErrorMessageDialog(ex);
			}
		}
	}

	@Override
	public JPanel createActionButtonsPanel() {
		// TODO Auto-generated method stub
		return null;
	}
}
